<?php
class Artmodel extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}

	public function init(){
		$dbHandle = $this->load->database('artdex', TRUE);
		if($dbHandle == ''){
			error_log('can not create db handle','dialdex');
			echo (print_r($dbHandle,true));
		}
		return $dbHandle;
	}
	
	public function userRegis($post)
	{
		$dbHandle = $this->init();
		
		if(isset($post['select']) && $post['select']=='aUser')
		{
		$Fname=$post['Fname'];
		$Lname=$post['Lname'];
		$Email=$post['Email'];
		$password=convert_uuencode($post['Password']);
		$sex=$post['sex'];
	    $insert="INSERT INTO `art_user_details` (`name` ,`lname` ,`gender` ,`email`,`user_name`,`pssword`,`status`)VALUES ('$Fname', '$Lname', '$sex', '$Email' ,'$Email','".addslashes($password)."','Inactive')";
	    error_log('ffffff '.$insert);
	    $result=mysql_query($insert) or die(mysql_error());
	    $userID= mysql_insert_id();
	    return $userID;
		}}
	   public function artistInsert($post)
	   {    $dbHandle = $this->init();
	    	$Fname=$post['Fname'];
	    	$Lname=$post['Lname'];
	    	$Email=$post['Email'];
	    	$password=convert_uuencode($post['Password']);
	    	$sex=$post['sex'];
	    	$insertartist="INSERT INTO `art_artist` (`fname`,`lname`,`gender`, `email`,`password`,`status`) VALUES ('$Fname','$Lname','$sex','$Email','$password','Inactive')";
	    	error_log('ffffff '.$insertartist);
	    	$result=mysql_query($insertartist) or die(mysql_error());
	    	$artistID= mysql_insert_id();
	    	return $artistID;
	     }
	     public function UpdateartistStatus($Aid)
	     {
	     	$dbHandle = $this->init();
	      	$updateartist="Update `art_artist` set status='Active' where artist_id='$Aid'";
	     	error_log('ffffff '.$updateartist);
	     	$result=mysql_query($updateartist) or die(mysql_error());
	     }     
	    
      public function userLogin($post)
	      {
	      	$dbHandle = $this->init();
	      	if(isset($post['userid']) && $post['userid']!=''){
	      		$email = $post['userid'];
	      	}else{
	      		$email=$post['txtEmail'];
	      	}
	      	if(isset($post['password']) && $post['password']!=''){
	      		$password=$post['password'];
	      	}else{
	      		$password=$post['txtPass'];
	      	}
		    
	        $query = "SELECT * FROM art_user_details where email='$email' and pssword='".addslashes(convert_uuencode($password))."'and status='Active'";
		    error_log('hh   '.$query);
		    $result = mysql_query($query);
		    $userArray = array();
		    $i=0;
		     while($row = mysql_fetch_assoc($result)){
			$userArray[$i] = $row;
			$i++;
		      }
		      
		     return $userArray;
	      }
	      
	  public function advertiserLogin($post)
	      {
	      	$dbHandle = $this->init();
	      	if(isset($post['userid']) && $post['userid']!=''){
	      		$email = $post['userid'];
	      	}else{
	      		$email=$post['txtEmail'];
	      	}
	      	if(isset($post['password']) && $post['password']!=''){
	      		$password=$post['password'];
	      	}else{
	      		$password=$post['txtPass'];
	      	}
	      
	      	$query = "SELECT * FROM art_advertise where email='$email' and password='".addslashes(convert_uuencode($password))."'and status='Active'";
	      	error_log('hh   '.$query);
	      	$result = mysql_query($query);
	      	$userArray = array();
	      	$i=0;
	      	while($row = mysql_fetch_assoc($result)){
	      		$userArray[$i] = $row;
	      		$i++;
	      	}
	      
	      	return $userArray;
	      }
	     public function PartnerLogin($post)
	      {
	      	$dbHandle = $this->init();
	      	if(isset($post['userid']) && $post['userid']!=''){
	      		$email = $post['userid'];
	      	}else{
	      		$email=$post['txtEmail'];
	      	}
	      	if(isset($post['password']) && $post['password']!=''){
	      		$password=$post['password'];
	      	}else{
	      		$password=$post['txtPass'];
	      	}
	      	 
	      	$query = "SELECT * FROM art_partner where email='$email' and password='$password' and status='Active'";
	      	error_log('hh   '.$query);
	      	$result = mysql_query($query);
	      	$userArray = array();
	      	$i=0;
	      	while($row = mysql_fetch_assoc($result)){
	      		$userArray[$i] = $row;
	      		$i++;
	      	}
	      	 
	      	return $userArray;
	      }
	      
	  public function chkEmail($post)
	      {
	      	$dbHandle = $this->init();
	      	$email=$post['Email'];
	      	$query = "SELECT * FROM art_user_details where email='$email'";
	      	error_log('hh   '.$query);
	      	$result = mysql_query($query);
	      	$userArray = mysql_fetch_row($result);
	      	return $userArray;
	      }
	      
      public function mailUpdate($uId)
	    {
		$dbHandle = $this->init();
		//$aid=$post['gallery_id'];
		$update = "update art_user_details set status='Active' where user_Id='$uId'";
		$updateresult = mysql_query($update);
		error_log('hiiiiiiiiiiiiiiiii   '.$update);
	    }
            
         public function latestPainting() {
        $dbHandle = $this->init();
        $query = "SELECT * from art_artist ar,art_gallery ag ,art_gallery_mapping agm where agm.artist_id=ar.artist_id and agm.Gallery_id=ag.gallery_Id order by gallery_uploaddate desc";
        $result = mysql_query($query);
        $resultArray = array();
        $i = 0;
        while ($row = mysql_fetch_assoc($result)) {
            $resultArray[$i] = $row;
            $i++;
        }
        return $resultArray;
    }
    
    public function forgetPassword($post)
    {
    	$dbHandle = $this->init();
    	$Email=$post['forgrtmail'];
        $query = "SELECT * FROM art_user_details where email='$Email' and status='Active'";
	    error_log('hh   '.$query);
		$result = mysql_query($query);
		    $userArray = array();
		    $i=0;
		     while($row = mysql_fetch_assoc($result)){
			$userArray[$i] = $row;
			$i++;
		      }
		      
		     return $userArray;
    }
    public function UpdateToPassword($post)
    {
    	$dbHandle = $this->init();
    	$user_id=$post['user_id'];
    	$Password=$post['forgrtPassword'];
    	$update = "update art_user_details set pssword='".$Password."' where user_id='$user_id'";
    	$updateresult = mysql_query($update);
    	error_log('hiiiiiiiiiiiiiiiii   '.$update);
    }
    
    public function getAllCms($cmsPage) {
        $dbHandle = $this->init();
        $query = "SELECT * from art_cms where title = '$cmsPage'";
        $result = mysql_query($query);
        $resultArray = array();
        $i = 0;
        while ($row = mysql_fetch_assoc($result)) {
            $resultArray[$i] = $row;
            $i++;
        }
        return $resultArray;
        
       // error_log('cms error......  ' . $query);
    }
    
    public function getAllArtistNane() {
        $dbHandle = $this->init();
        $query = "SELECT artist_id,fname,lname from art_artist LIMIT 0 , 12";
        $result = mysql_query($query);
        $resultArray = array();
        $i = 0;
        while ($row = mysql_fetch_assoc($result)) {
            $resultArray[$i] = $row;
            $i++;
        }
        return $resultArray;
        
       // error_log('cms error......  ' . $query);
    }
    
    public function getAllTheme() {
        $dbHandle = $this->init();
        $query = "SELECT * from art_theme LIMIT 0 , 12";
        $result = mysql_query($query);
        $resultArray = array();
        $i = 0;
        while ($row = mysql_fetch_assoc($result)) {
            $resultArray[$i] = $row;
            $i++;
        }
        return $resultArray;
        
       // error_log('cms error......  ' . $query);
    }

    public function getAllMedium() {
        $dbHandle = $this->init();
        $query = "SELECT * from  art_medium LIMIT 0 , 12";
        $result = mysql_query($query);
        $resultArray = array();
        $i = 0;
        while ($row = mysql_fetch_assoc($result)) {
            $resultArray[$i] = $row;
            $i++;
        }
        return $resultArray;
        
       // error_log('cms error......  ' . $query);
    }

    public function getAllCatagories() {
        $dbHandle = $this->init();
        $query = "SELECT * from art_category LIMIT 0 , 12";
        $result = mysql_query($query);
        $resultArray = array();
        $i = 0;
        while ($row = mysql_fetch_assoc($result)) {
            $resultArray[$i] = $row;
            $i++;
        }
        return $resultArray;
        
       // error_log('cms error......  ' . $query);
    }
    
      public function getCountry()
    {
    	$dbHandle = $this->init();
        $query = "SELECT * FROM art_country";
	    error_log('hh   '.$query);
		$result = mysql_query($query);
		    $userArray = array();
		    $i=0;
		     while($row = mysql_fetch_assoc($result)){
			$userArray[$i] = $row;
			$i++;
		      }
		      
		     return $userArray;
    }
    
    ///Artist Managment
    public function insertAdvertiser($post)
    {
    	    $dbHandle = $this->init();
    	    $Fname=$post['Fname'];
	     	$Lname=$post['Lname'];
	     	$Email=$post['Email'];
	     	$password=convert_uuencode($post['Password']);
	     	$sex=$post['sex'];
	     	$address=$post['Address'];
	     	$city=$post['cityID'];
	     	$state=$post['stateID'];
	     	$country=$post['CountryID'];
	     	$Pin=$post['Pin'];
	     	$mobile=$post['Mobile'];
            $advertise="INSERT INTO `art_advertise` (`fname`, `lname`, `gender`, `address`, `city`, `state`, `country`,`pin`,`mobile`, `phone`, `email`, `password`, `status`) VALUES ('$Fname', '$Lname', '$sex', '$address', '$city', '$state', '$country','$Pin','$mobile','$mobile','$Email','$password','Inactive')";
            error_log('ffffff '.$advertise);
	     	$result=mysql_query($advertise) or die(mysql_error());
	     	$adveritsertID= mysql_insert_id();
	     	return $adveritsertID;
	      
}
    public function updateMailAdvertise($Aid)
     {
	   $dbHandle = $this->init();
	     $update = "update art_advertise set status='Active' where ad_Id='$Aid'";
		$updateresult = mysql_query($update);
		error_log('hiiiiiiiiiiiiiiiii   '.$update);
	 
         }
         public function chkEmailArtist($post)
         {
         	$dbHandle = $this->init();
         	$email=$post['Email'];
         	$query = "SELECT * FROM art_artist where email='$email'";
         	error_log('hh   '.$query);
         	$result = mysql_query($query);
         	$userArray = mysql_fetch_row($result);
         	return $userArray;

     }
     public function artistLogin($post)
     {
     	$dbHandle = $this->init();
     	if(isset($post['userid']) && $post['userid']!=''){
     		$email = $post['userid'];
     	}else{
     		$email=$post['txtEmail'];
     	}
     	if(isset($post['password']) && $post['password']!=''){
     		$password=$post['password'];
     	}else{
     		$password=$post['txtPass'];
     	}
     	 
     	$query = "SELECT * FROM art_artist where email='$email' and password='".addslashes(convert_uuencode($password))."'and status='Active'";
     	error_log('hh   '.$query);
     	$result = mysql_query($query);
     	$userArray = array();
     	$i=0;
     	while($row = mysql_fetch_assoc($result)){
     		$userArray[$i] = $row;
     		$i++;
     	}
     	 
     	return $userArray;
     }
     
     public function getAllCountry()
     {
     	$dbHandle = $this->init();
     	$query = "SELECT * from art_country ";
     	$result = mysql_query($query);
     	$resultArray = array();
     	$i = 0;
     	while ($row = mysql_fetch_assoc($result)) {
     		$resultArray[$i] = $row;
     		$i++;
     	}
     	return $resultArray;
     	
     	
     	
     }
     public function getAllState()
     {
     	$dbHandle = $this->init();
     	$query = "SELECT * from art_states ";
     	$result = mysql_query($query);
     	$resultArray = array();
     	$i = 0;
     	while ($row = mysql_fetch_assoc($result)) {
     		$resultArray[$i] = $row;
     		$i++;
     	}
     	return $resultArray;
     }
     public function getAllCity()
     {
     	$dbHandle = $this->init();
     	$query = "SELECT * from art_city ";
     	$result = mysql_query($query);
     	$resultArray = array();
     	$i = 0;
     	while ($row = mysql_fetch_assoc($result)) {
     		$resultArray[$i] = $row;
     		$i++;
     	}
     	return $resultArray;
     
     
     
     }
     public function selecStateById($cId)
     {
     	$dbHandle=$this->init();
     	$query="SELECT * FROM art_states , art_state_country  where art_states .stateID=art_state_country.state_id and art_state_country.country_id='$cId'";
     	error_log('theme   '.$query);
     	$result=mysql_query($query);
     	$resultArray = array();
     	$i=0;
     	while($row = mysql_fetch_assoc($result))
     	{
     		$resultArray[$i] = $row;
     		$i++;
     	}
     	return $resultArray;
     }
     public function selecCityById($cId)
     {
     	$dbHandle=$this->init();
     	$query="SELECT * FROM  art_city ,art_city_state  where art_city .cityID=art_city_state.city_id and art_city_state.state_id='$cId'";
     	error_log('theme   '.$query);
     	$result=mysql_query($query);
     	$resultArray = array();
     	$i=0;
     	while($row = mysql_fetch_assoc($result))
     	{
     		$resultArray[$i] = $row;
     		$i++;
     	}
     	return $resultArray;
     }
     public function InsertAdvertisment($post)
     {
     	   $dbHandle = $this->init();
     	    $ad_Id=$post['ad_Id'];
    	    $AdCategory=$post['AdCategory'];
	     	//$Adimage=$post['Adimage'];
	     	$Adcontent=$post['Adcontent'];
	       	$Adprice=$post['Adprice'];
	       	$image = time().$post['file']["photo"]["name"];
	       	move_uploaded_file($post['file']["photo"]["tmp_name"], "public/images".$image);
            $advertisement="INSERT INTO `art_ads_advertise` (`ads_category` ,`image` ,`content` ,`price` )VALUES ('$AdCategory', '$image', '$Adcontent', '$Adprice')";
            error_log('ffffff '.$advertisement);
	     	$result=mysql_query($advertisement) or die(mysql_error());
	     	$adsID= mysql_insert_id();
	     	$Adsmapping="insert into art_advertise_mapping(ad_id,ads_id)values('$ad_Id','$adsID')";
	     	error_log('ffffff '.$Adsmapping);
	     	$result=mysql_query($Adsmapping) or die(mysql_error());
	     	return $adsID;
     }
     public function getAdvertiserDetail($ad_Id)
     {
     	$dbHandle=$this->init();
     	$query="select a.*, c.countryName, ct.cityName, s.stateName from  art_advertise a, art_country c, art_states s, art_city ct where a.ad_Id='$ad_Id' and a.country=c.countryID and a.state=s.stateID and a.city=ct.cityID";
     	//error_log('theme   '.$query);
     	$result=mysql_query($query);
     	$resultArray = array();
     	$i=0;
     	while($row = mysql_fetch_assoc($result))
     	{
     		$resultArray[$i] = $row;
     		$i++;
     	}
     	return $resultArray;
     }
     public function AdvertismentDetail($adsID)
     {
     	$dbHandle=$this->init();
     	$query="select * from art_ads_advertise where ads_Id='$adsID'";
     	//error_log('theme   '.$query);
     	$result=mysql_query($query);
     	$resultArray = array();
     	$i=0;
     	while($row = mysql_fetch_assoc($result))
     	{
     		$resultArray[$i] = $row;
     		$i++;
     	}
     	return $resultArray;
     }
}